***************************************************************************
* This program creates the tables and figures for "The Self-Constrained Hand-to-Mouth" by Michael Gelman
* 01/20/2021
******************************************************************************

/*NOTES
***************************************************************************
Software used: Stata version 16 on Linux 
****************************************************************************/
clear
set more off

*set global vars
	global new_base = "~/mgelman/data/SelfConstrained/rep"
	global fig "fig"
	global tab "tab"
	global plot_args vert recast(line) nooffset graphregion(color(white)) bgcolor(white) 
	global plot_opt = "lcolor(black) ciplot(rline) acolor(gray) alpattern(dot)" 
	

*0000000000000000000000
*STEP 1: LOAD AND PROCESS ALL OF THE CORE TX DATA FROM SQL
*0000000000000000000000
	cap program drop _all
	*loop through partitions
	forval i = 0/9 {
		*define vars
			*paycheck
				local paycheck_raw $new_base/raw/paycheck_p`i'.tsv
				local paycheck_dta $new_base/dta/aux/paycheck_p`i'.dta 
			*expenditures
				local expenditures_raw $new_base/raw/daily_expenditures_p`i'.tsv
				local expenditures_dta $new_base/dta/aux/daily_expenditures_p`i'.dta
			*liquidity
				local liq_raw $new_base/raw/daily_liq_balance_`i'.tsv
				local daily_liq_dta $new_base/dta/aux/daily_balance_`i'.dta
			*refund 
				local refund_raw $new_base/raw/refund_`i'.tsv
				local refund_dta $new_base/dta/aux/refund_`i'.dta
			*ML files
				local cat_raw $new_base/../../ML/restaurant/out/rest_type_hat`i'.csv
				local cat_dta $new_base/dta/aux/spending_rest_hat`i'.dta
			*merged file
				local merged_dta $new_base/dta/aux/merged_data_p`i'.dta
			*payroll schedule file
				local payroll_file $new_base/dta/aux/bi_weekly_strict_by_odd_`i'.dta
			*processed file
				local after_collapse $new_base/dta/analysis_after_collapse_`i'.dta 
		*load liquidity data [rawfile, daily_dtafile, partition]
			SQL_data_load "load_liquidity_data" `liq_raw' `daily_liq_dta' p`i'
		*load paycheck date [rawfile, dtafile, partition]
			SQL_data_load "load_paycheck_data" `paycheck_raw' `paycheck_dta' p`i'
		*load daily expenditures data that takes out CC spending, TX, and selects on sample [rawfile, dtafile]
			SQL_data_load "load_expenditures_data" `expenditures_raw' `expenditures_dta' p`i'
		*load refund data [rawfile, dtafile]
			SQL_data_load "load_refund_data" `refund_raw' `refund_dta' p`i'
		*load ML data
			SQL_data_load "load_cat_data" `cat_raw' `cat_dta'
		*combine files
			SQL_data_load "combine_data" `paycheck_dta' `expenditures_dta' `daily_liq_dta' `cat_dta' `merged_dta'
		*process combined files
			SQL_data_load "process_combined_data" `merged_dta' `payroll_file' `after_collapse '
	}
	*append tax refund data
		SQL_data_load "append_data" "$new_base/dta/aux/refund_"
	*append all the processed data
		SQL_data_load "append_data" "$new_base/dta/analysis_after_collapse_"

*0000000000000000000000
*STEP 2: COMPARITIVE MONTHLY TIME SERIES ANALYSIS (FIG 1)
*0000000000000000000000
	*import monthly census data
	*CENSUS DATA FROM 	https://www.census.gov/econ/currentdata/dbsearch?program=MRTS&startYear=2013&endYear=2017&categories=7221&dataType=SM&geoLevel=US&notAdjusted=1&submit=GET+DATA&releaseScheduleId=
		insheet using ~/Dropbox/git_repo/SelfConstrained/xls/census_data_ff_rest.csv, clear		
		*create month
			ren date date_str
			gen date= date(date_str,"MY")
			gen mofd = mofd(date)
				format mofd %tm
		*delete other vars
			drop date_str date
		*save down
			save $new_base/dta/census_data_ff_rest.dta, replace
	
	*Use data before collapsing and processing
		use user_id x_1 date using $new_base/dta/aux/merged_data_p0.dta, clear	
			forval i = 1/9{			
			 append using $new_base/dta/aux/merged_data_p`i'.dta, keep(user_id x_1 date )
			}

	*rename the food variable to make it more descriptive
		ren x_1 x_food
		keep user_id x_food date
		gen mofd = mofd(date)
			format mofd %tm
	*sum to the monthly-user level and take logs
		collapse (sum) x_food, by(mofd user_id)
			gen log_x_food = log(x_food)
	*drop first and last month
		egen min_week = min(mofd), by(user_id)
		egen max_week = max(mofd), by(user_id)
			drop if inlist(mofd,min_week,max_week)
	*take monthly average
		collapse log_x_food , by(mofd)
	*merge in census data
		qui mmerge mofd using $new_base/dta/census_data_ff_rest.dta, t(1:1)
				keep if _merge == 3
					drop _merge
	*create log changes relative to jan 2013
		*create log census
			gen log_census = ln(census)
		*create variable for jan 2013
			cap drop jan_app*
			foreach type in log_x_food log_census {
				gen `type'_tmp = `type' if mofd == tm(2013m1)
				egen jan_`type' = max(`type'_tmp)
					drop `type'_tmp
				gen `type'_diff = `type'- jan_`type'
				}
	*0000000000000
	*FIGURE 1: Monthly food expenditures
	*0000000000000
		line *_diff mofd, ///
			legend(order(1 "App data" 2 "Census data")) ytitle("Log difference relative to Jan 2013") xtitle("Month") graphregion(color(white)) bgcolor(white)  lwidth(thick thick) ///
			xlabel(636(6)671, angle(90)) lcolor(black black) lpattern(solid dash)
			graph export  $fig/time_series_app_census.eps, replace

*0000000000000000000000
*STEP 3: DESCRIPTIVE STATS (TABLE 1 AND 2)
*0000000000000000000000
	*load data and create variables used in the analysis
		*load data
			use $new_base/dta/analysis_after_collapse_merged.dta, clear	
		*keep relevant vars
			keep user_id week_date x x_1 y y_paycheck freq liquid_balance odd date
		*keep weekly and bi-weekly (drop monthly and semi-monthly)
			drop if inlist(freq,1,2)
		*drop bi-weekly if we don't know the schedule
			drop if freq == 3 & mi(odd)
		*create log food
			ren x_1 x_food //rename first to make the var more descriptive
			gen log_x_food= log(x_food)					
		*set up panel structure
			cap drop user_int
			egen user_int = group(user_id)
			tsset user_int week_date
		*create log differences
			gen diff_log_x_food = F.log_x_food - log_x_food
		*create different pay schedule
			replace freq = 30 if odd == 0 & freq == 3
			replace freq = 31 if odd == 1 & freq == 3
		*create normalized 2013 liquidity quantile
			gen year = year(date)
			egen tag = tag(user_int)
			*create avg spending and normalized liquidity
				egen x_bar_tmp = mean(x) if year == 2013, by(user_id)
					egen x_bar = max(x_bar_tmp), by(user_id)
				egen avg_liq_tmp = mean(liquid_balance) if year == 2013, by(user_id)
					egen avg_liq_2013 = max(avg_liq_tmp ), by(user_id)
				gen avg_liquidity_norm = avg_liq_2013 / x_bar
				xtile liq_xtile_tmp = avg_liquidity_norm if tag , nq(3)
					egen liq_xtile = max(liq_xtile_tmp), by(user_int)
					drop liq_xtile_tmp
	*0000000000000
	*SUMMARY STATS
	*0000000000000
		*create monthly data
			preserve
			*keep biweekyl paycheck indivdiuals
				keep if inlist(freq,30,31) 
			*take floor and divide by 7 to get week
				gen month_date = floor(week_date/4)				
		*collapse to the monthly level
			collapse (sum) x_food y y_paycheck (mean) liquid_balance (min) date, by(user_id month_date)				
		*drop min and max week per person
			egen min_month = min(month_date), by(user_id)
			egen max_month = max(month_date), by(user_id)
				drop if inlist(month_date,min_month,max_month)
				drop min_month max_month		
		*basic stats (total food income liquid balance)	
			foreach type in x x_food y y_paycheck liquid_balance  {			
				cap drop `type'_sumwin
				winsor `type' , gen(`type'_sumwin) p(0.01)
				}
				
			label var x_food_sumwin "Food expenditures ($)"
			label var y_sumwin "Income ($)"
			label var y_paycheck_sumwin "Paycheck income ($)"
			label var liquid_balance_sumwin "Liquid balance ($)"
			
			qui estpost sum x_food_sumwin y_sumwin y_paycheck_sumwin liquid_balance_sumwin , det		
		*000000000000000000000000000000000000000000000000000
		*TABLE 2: SUMMARY STATS
		*000000000000000000000000000000000000000000000000000
			esttab using "$tab/summary_stats.tex", cells("mean(fmt(%9.0fc)) p25(fmt(%9.0fc)) p50(fmt(%9.0fc)) p75(fmt(%9.0fc))") substitute(_ \_ $ \\$) label nonum noobs replace ///
				postfoot(\hline \end{tabular}})
			distinct user_id
			restore
		
	*00000000000000000000000000
	*PAYCHECK SHARE ANALYSIS
	*00000000000000000000000000
		*create pay week
			gen odd_week = mod(week_date,2)	
			cap drop pay_week
				gen pay_week = odd_week
				replace pay_week = 1 - odd_week if freq == 30	
				replace pay_week =0 if !inlist(freq,30,31)
		*create winsorized versions of income
			foreach var in y y_paycheck {
				winsor `var', p(0.01) gen(`var'_win)
				}
		*indv level avg y
			egen y_avg = mean(y_win), by(user_id)
			cap drop pay_share
				gen pay_share = y_paycheck_win/y_avg 
			cap drop y_no_pay non_pay_share
				gen y_no_pay_win=y_win-y_paycheck_win
				gen non_pay_share = y_no_pay_win/y_avg
				
		*000000000000000000000000000000000000000000000000000
		*TABLE 1: Income share for bi-weekly paycheck receivers
		*000000000000000000000000000000000000000000000000000	
			*pay and non pay breakdown
				tabstat pay_share non_pay_share if inlist(freq,30,31), by(pay_week) format(%4.2f)
			*how many bi weekly users?
				distinct user_id if inlist(freq,30,31)
		*000000000000000000000000000000000000000000000000000
		*TABLE A2: Income share for bi-weekly paycheck receivers by pay schedule
		*000000000000000000000000000000000000000000000000000	
			*pay and non pay breakdown (by schedule)
				tabstat pay_share non_pay_share if freq==31, by(pay_week) format(%4.2f)
				tabstat pay_share non_pay_share if freq==30, by(pay_week) format(%4.2f)
				tab freq if tag
*0000000000000000000000
*STEP 4: EXCESS SENSITIVITY TIME SERIES ANALYSIS (FIG 2,3a)
*0000000000000000000000
			
*00000000000000000000000000000000
*TIME SERIES PLOTS
*00000000000000000000000000000000
	*weekly label
			global date_label = ""
			local j = 19333
			forval i = 0(1)190{
				local j = 19333 + 7*`i'
				global month = month(`j') 
				global day = day(`j') 
				global year = year(`j') 
				global date_str = "$month/$day/$year"
					if mod(`i',4)==0 {
						global date_label = `"`i'.week_date = $date_str"' +  `" $date_label"'
						}
					*else if mod(`i',4)!=0{
					else {
						global date_label = `"`i'.week_date = `" "'"' + `" $date_label"'
					}
				}			
	*000000000000000000000000000000000000000000000000000
	*FIGURE 2: WEEKLY FOOD EXPENDITURES
	*000000000000000000000000000000000000000000000000000
		gen month = mofd(date)
		winsor x_food, gen(x_food_win) p(0.01)

		preserve
			keep if month >= tm(2014m3) &  month <=tm(2014m9)
				qui reg x_food_win ibn.week_date if freq == 31 , noconst 
					est store odd
				qui reg x_food_win ibn.week_date if freq == 30 , noconst 
					est store even
				qui reg x_food_win ibn.week_date if freq == 4, noconst 
					est store weekly
			coefplot (odd, label(Bi-weekly paycheck: Odd schedule) lwidth(thick) lp(dash) lcolor(black) ) ///
			(even, label(Bi-weekly paycheck: Even schedule) lwidth(thick) lp(shortdash) lcolor(black)) ///
			(weekly, label(Weekly paycheck) lp(solid) lwidth(thick) lcolor(black)),  ///
			noci  $plot_args xlabel(,angle(90) notick ) coeflabel($date_label)  ytitle("Food expenditures (weekly)")  legend(rows(3))
				graph export  $fig/weekly_spending.eps, replace
		restore	
	*000000000000000000000000000000000000000000000000000
	*FIGURE 3: WEEKLY TIME SERIES FOR BI-WEEKLY PAYCHECK RECEIVERS (HETEROGENEITY)
	*000000000000000000000000000000000000000000000000000
		preserve
			*KEEP MARCH TO SEPTEMBER 2014
				keep if month >= tm(2014m3) &  month <=tm(2014m9)
			*AVERAGE ACROSS INDIVIDUALS BY LIQ TERCILE
				collapse (min) date (mean) x_food_win if freq == 31, by(week_date liq_xtile) 
					drop if mi(liq_xtile)
				reshape wide x_food_win , i(week_date) j(liq_xtile)
			*CALCUALTE LOG DEVIATIONS
				forval i = 1/3 {
						gen lx_food`i' = log(x_food_win`i')
						egen mean`i' = mean(lx_food`i')
						gen dm`i' = lx_food`i' - mean`i'
					}
			*PLOT THE TIME SERIES
				line dm* date, xlabel(,angle(90)) xtitle("")  legend(cols(3)) lwidth(thick thick thick) lcolor(b b b)  lpattern(solid dash shortdash ) ///
					legend(order(1 "Low liquidity" 2 "Medium liquidity" 3 "High liquidity")) graphregion(color(white)) bgcolor(white) ytitle("Log deviations")  ylabel(-0.15(0.05)0.15)
					graph export  $fig/weekly_spending_liq.eps, replace
		restore
		
*0000000000000000000000
*STEP 5: IMPACT OF PAYROLL SCHEDULE (TABLE 3 AND 4)
*0000000000000000000000
	*create even week and label
		gen even_week = 1-odd_week
			label var even_week "$ Even_{it} $"
			
			cap drop pay_even pay_odd
			gen pay_even = pay_week
				replace pay_even = 0 if freq!=30
			gen pay_odd = pay_week
				replace pay_odd = 0 if freq!=31
	*label variables
		label var log_x_food "$ ln(Food_{it}) $"
		label var even_week "$ Even_{t} $"
		label var pay_even "$ Payweek^{Even}_{it} $"
		label var pay_odd "$ Payweek^{Odd}_{it} $"
		label var pay_week "$ Payweek_{it} $"

	*000000000000000000000000000000000000000000000000000
	*TABLE 3: EXCESS SENSITIVITY ESTIMATES
	*000000000000000000000000000000000000000000000000000
		*baseline reg [broken down into bi-weekly schedule] (column 1 of table 3)
			qui areg log_x_food even_week pay_even pay_odd, a(user_id) robust cluster(user_id)
				test pay_even = pay_odd
				outreg2 using $tab/excess_sensitivity.tex, nocons tex(frag) dec(3) replace label nonotes
		*pooled baseline reg (column 2 of table 3)
			qui areg log_x_food even_week pay_week, a(user_id) robust cluster(user_id)	
				outreg2 using $tab/excess_sensitivity.tex, nocons tex(frag) dec(3) label  nonotes		
	*000000000000000000000000000000000000000000000000000
	*TABLE 4: EXCESS SENSITIVITY ESTIMATES BY LIQUIDITY TERCILE
	*000000000000000000000000000000000000000000000000000		
	*baseline reg with liquidity breakdown (only use 2014 and after data)
	preserve
		keep if year>=2014
			qui areg log_x_food even_week pay_even pay_odd if liq_xtile==1, a(user_id) robust cluster(user_id)
				outreg2 using $tab/excess_sensitivity_liq.tex, nocons tex(frag) dec(3) replace label ctitle(Low avg liquidity) nonotes
			qui areg log_x_food even_week pay_even pay_odd if liq_xtile==2, a(user_id) robust cluster(user_id)
				outreg2 using $tab/excess_sensitivity_liq.tex, nocons tex(frag) dec(3) label ctitle(Medium avg liquidity) nonotes
			qui areg log_x_food even_week pay_even pay_odd if liq_xtile==3, a(user_id) robust cluster(user_id)
				outreg2 using $tab/excess_sensitivity_liq.tex, nocons tex(frag) dec(3) label ctitle(High avg liquidity) nonotes
	restore

*0000000000000000000000
*STEP 6: EULER EQUATION TEST (FIGURE 5,7) (TABLE 6)
*0000000000000000000000
	*set up panel and create log liquidity
		tsset user_int week_date		
		gen log_liq = log(L.liquid_balance)	

	*000000000000000000000000000000000000000000000000000
	*FIGURE 5: EULER EQUATION TEST (PAY WEEK)
	*000000000000000000000000000000000000000000000000000
	*residuals conditional on that pay period
		qui areg log_liq if pay_week == 1 & year>=2014 , a(user_int)
			predict log_liq_resid_pay, resid
	*plot
		preserve
		keep if year>=2014 & inlist(freq,30,31)
		
		tw (lpolyci diff_log_x_food log_liq_resid_pay if log_liq_resid_pay >= -2 & log_liq_resid_pay <=2 & pay_week == 1 & liq_xtile == 1, lwidth(thick)  $plot_opt )  ///
		(lpolyci diff_log_x_food log_liq_resid_pay if log_liq_resid_pay >= -2 & log_liq_resid_pay <=2 & pay_week == 1 & liq_xtile == 2, lpattern(dash) lwidth(thick) $plot_opt) ///
		(lpolyci diff_log_x_food log_liq_resid_pay if log_liq_resid_pay >= -2 & log_liq_resid_pay <=2 & pay_week == 1 & liq_xtile == 3, lpattern(shortdash) lwidth(thick) $plot_opt) , ///
				legend(order(2 "Low average liquidity" 4 "Medium average liquidity" 6 "High average liquidity")) ytitle("Food expenditures growth") xtitle("Relative liquidity") ///
				graphregion(color(white)) bgcolor(white) ylabel(0.1(0.1)-0.2, angle(360)) 
				graph export  $fig/gc_pay_liq_xtile.eps, replace	
		restore
	*000000000000000000000000000000000000000000000000000
	*FIGURE 7: EULER EQUATION TEST (NON PAY WEEK)
	*000000000000000000000000000000000000000000000000000
	*residuals conditional on that pay period
		qui areg log_liq if pay_week == 0 & year>=2014 , a(user_int)
		cap drop log_liq_resid_nopay
		predict log_liq_resid_nopay, resid
	*plot
		preserve
		keep if year>=2014  & inlist(freq,30,31)
		
		tw (lpolyci diff_log_x_food log_liq_resid_nopay if log_liq_resid_nopay >= -2 & log_liq_resid_nopay <= 2 & pay_week == 0 & liq_xtile == 1, lwidth(thick) $plot_opt )  ///
		   (lpolyci diff_log_x_food log_liq_resid_nopay if log_liq_resid_nopay >= -2 & log_liq_resid_nopay <= 2 & pay_week == 0 & liq_xtile == 2, lpattern(dash) lwidth(thick) $plot_opt) ///
		   (lpolyci diff_log_x_food log_liq_resid_nopay if log_liq_resid_nopay >= -2 & log_liq_resid_nopay <= 2 & pay_week == 0 & liq_xtile == 3, lpattern(shortdash) lwidth(thick) $plot_opt) , ///
				legend(order(2 "Low average liquidity" 4 "Medium average liquidity" 6 "High average liquidity")) ytitle("Food expenditures growth") xtitle("Relative liquidity") ///
				graphregion(color(white)) bgcolor(white) ylabel(-0.1(0.1)0.2, angle(360)) 
					graph export  $fig/gc_nopay_liq_xtile.eps, replace
		restore
	*000000000000000000000000000000
	*REGRESSION OUTPUT
	*000000000000000000000000000000
		gen log_liq_pay = log_liq
			replace log_liq_pay = 0 if pay_week==0
		gen log_liq_nopay = log_liq
			replace log_liq_nopay = 0 if pay_week==1
		label var log_liq_pay "Pay week"
		label var log_liq_nopay "Non pay week"
		
		preserve
		keep if year>=2014 & inlist(freq,30,31)
			qui reghdfe diff_log_x_food  log_liq_pay log_liq_nopay  if year>=2014 & liq_xtile==1, a(user_int#pay_week) vce(cluster user_int)
				outreg2 using $tab/gc_by_liq.tex, nocons tex(frag) dec(3) replace label ctitle(Low avg liquidity) nonotes
			qui reghdfe diff_log_x_food  log_liq_pay log_liq_nopay  if year>=2014 & liq_xtile==2, a(user_int#pay_week) vce(cluster user_int)
				outreg2 using $tab/gc_by_liq.tex, nocons tex(frag) dec(3) label ctitle(Medium avg liquidity) nonotes
			qui reghdfe diff_log_x_food  log_liq_pay log_liq_nopay  if year>=2014 & liq_xtile==3, a(user_int#pay_week) vce(cluster user_int)
				outreg2 using $tab/gc_by_liq.tex, nocons tex(frag) dec(3) label ctitle(High avg liquidity) nonotes
		restore

*0000000000000000000000
*STEP 7: EFFECT OF REFUND (FIGURE 10,11,A.2) (TABLE 7,8)
*0000000000000000000000
	*0000000000000000
	*Add in tax refunds
	*0000000000000000
		qui mmerge user_id week_date using $new_base/dta/aux/refund_merged, t(1:1)
			drop if _merge == 2 
		gen refund_week = _merge == 3
	*000000000000000000000000000000000000000000000000000
	*FIGURE A2: CONSUMPTION RESPONSE
	*000000000000000000000000000000000000000000000000000
	*IRF
		tsset user_int week_date
		cap drop IRF 
		gen IRF = 0 if !mi(refund_week)
	
		forval i = 1/10 {
			replace IRF =  `i' if L`i'.refund_week==1 
			replace IRF =  -`i' if F`i'.refund_week==1
		}	
	
		*create IRF variable with no negative values
			cap drop IRF2
			gen IRF2 = IRF + 10
	*label
	global day_label = ""
	forval i = 0(1)22{
		local j = `i' - 10
				global day_label = `"`i'.IRF2 = `j'"' +  `" $day_label"'
		}
		di "$day_label"
	
	*create figure	
		qui areg log_x_food i.week_date ib10.IRF2  , a(user_int) robust cluster(user_int)
			coefplot , keep(*IRF2*)  vertical recast(connected)  omit base  ///
						graphregion(color(white)) bgcolor(white)   nooffset  mcolor(black) lcolor(black) ciopts(recast(rline) lpattern(dash) lcolor(gray))  ///
						ytitle("Spending response") xtitle("Weeks since Tax Refund") ///
						coeflabel($day_label) ylabel(0(0.02)0.14) 
						graph export  $fig/food_refund_IRF.eps, replace	

/*Note: Tax refunds tend to arrive on Mo,Tu,We with the peak on We. 
        Given that I define the paycheck to start on Thursday, the above figure shows that the impact of the tax refund tends to show up in the week after the refund is received. 
		This is just an artifact of the way I define the start of the week. In order to align the two concepts, I use a one period lag for the tax refund indicator.
*/
						
*create/label vars
	cap gen refund_ind = L.refund_week
		label var refund_ind "$ refund_{it} $"
	cap gen refund_ind_pay = refund_ind 
	replace refund_ind_pay = 0 if pay_week==0
		label var refund_ind_pay "$ refund_{it} \times payweek_{it} $"
	label var pay_week "$ payweek_{it} $"
	label var diff_log_x_food "$ \Delta ln(food_{it+1}) $"

*000000000000000000000000000000000000000000000000000
*TABLE 7: IMPACT OF REFUND RECEIPT ON EXPENDITURE GROWTH
*000000000000000000000000000000000000000000000000000
*only analyze biweekly paycheck receivers
keep if inlist(freq,30,31)
	qui areg diff_log_x_food refund_ind pay_week refund_ind_pay  if year>=2014 & liq_xtile==1, a(user_int)
		outreg2 using $tab/gc_by_ref_liq.tex, nocons tex(frag) dec(3) replace label ctitle(Low avg liquidity) nonotes
	qui areg diff_log_x_food refund_ind pay_week refund_ind_pay  if year>=2014 & liq_xtile==2, a(user_int)
		outreg2 using $tab/gc_by_ref_liq.tex, nocons tex(frag) dec(3) label ctitle(Medium avg liquidity) nonotes
	qui areg diff_log_x_food refund_ind pay_week refund_ind_pay  if year>=2014 & liq_xtile==3, a(user_int)
		outreg2 using $tab/gc_by_ref_liq.tex, nocons tex(frag) dec(3) label ctitle(High avg liquidity) nonotes

tsset user_int week_date
*000000000000000000000000000000000000000000000000000
*FIGURE 10: EULER EQUATION TEST (NON PAY WEEK)
*000000000000000000000000000000000000000000000000000
	tw (lpolyci diff_log_x_food log_liq_resid_nopay if log_liq_resid_nopay >= -2 & log_liq_resid_nopay <=2 & pay_week == 0, lwidth(thick) $plot_opt ) ///
	(lpolyci diff_log_x_food log_liq_resid_nopay if log_liq_resid_nopay >= -2 & log_liq_resid_nopay <=2 & pay_week == 0 & refund_ind==1, lwidth(thick) lpattern(dash) $plot_opt ),  ///
		legend(order(2 "No tax refund" 4 "Tax refund")) ytitle("Food expenditures growth") xtitle("Relative liquidity") graphregion(color(white)) bgcolor(white) /// 
			ylabel(-0.2(0.05)0.2, angle(360)) 
		graph export  $fig/gc_nopay_TR.eps, replace	
*000000000000000000000000000000000000000000000000000
*FIGURE 11: EULER EQUATION TEST (PAY WEEK)
*000000000000000000000000000000000000000000000000000
	tw (lpolyci diff_log_x_food log_liq_resid_pay if log_liq_resid_pay >= -2 & log_liq_resid_pay <=2 & pay_week == 1, lwidth(thick) $plot_opt ) ///
	(lpolyci diff_log_x_food log_liq_resid_pay if log_liq_resid_pay >= -2 & log_liq_resid_pay <=2 & pay_week == 1 & refund_ind==1, lwidth(thick) lpattern(dash) $plot_opt ),  ///
		legend(order(2 "No tax refund" 4 "Tax refund")) ytitle("Food expenditures growth") xtitle("Relative liquidity") graphregion(color(white)) bgcolor(white) /// 
			ylabel(-0.2(0.05)0.2, angle(360)) 
		graph export  $fig/gc_pay_TR.eps, replace	
*000000000000000000000000000000000000000000000000000
*TABLE 8: EULER EQUATION TEST RESULTS
*000000000000000000000000000000000000000000000000000
	*create liquidity variables
		gen log_liq_pay_ref2 = log_liq_pay
			replace log_liq_pay_ref2 = 0 if refund_ind == 0
		gen log_liq_nopay_ref2 = log_liq_nopay
			replace log_liq_nopay_ref2 = 0 if refund_ind == 0
	*define labels
		label var log_liq_pay "$ liq^{pay}_{it-1} $"
		label var log_liq_nopay "$ liq^{nopay}_{it-1} $"
		label var log_liq_pay_ref2 "$ liq^{pay}_{it-1} \times refund_{it} $"
		label var log_liq_nopay_ref2 "$ liq^{nopay}_{it-1} \times refund_{it} $"
	*estimate the specification and output table
		qui reghdfe diff_log_x_food log_liq_pay log_liq_pay_ref2 log_liq_nopay log_liq_nopay_ref2 refund_ind refund_ind_pay, a(user_int#pay_week) vce(cluster user_int)
					outreg2 using $tab/gc_by_ref.tex, nocons tex(frag) dec(3) replace label nonotes
	*test the significance of beta_3 + beta_4
		lincom log_liq_nopay + log_liq_nopay_ref2
